package com.mvd.app.activitys;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kongzue.dialog.interfaces.OnMenuItemClickListener;
import com.kongzue.dialog.util.TextInfo;
import com.kongzue.dialog.v3.BottomMenu;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mvd.app.R;
import com.mvd.app.ac2020.VideoListActivity;
import com.mvd.app.activity.BaseActivity;
import com.mvd.app.activity.WaiJianPhotoActivity;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.CommonUtils;
import com.mvd.app.commons.DateUtil;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.commons.SharedPreferencesUtils;
import com.mvd.app.commons.UtilsLog;
import com.mvd.app.model.CarItemStartModel;
import com.mvd.app.model.CarsInforModel;
import com.mvd.app.model.WaiJianAddRecordModel;
import com.mvd.app.model.WaiJianItemModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 外检 车辆详情 页面
 */

public class WaiJianActivity extends BaseActivity {

    @BindView(R.id.ac_waijian_number)
    TextView acWaijianNumber;
    @BindView(R.id.ac_waijian_SN)
    TextView acWaijianSN;
    @BindView(R.id.ac_waijian_times)
    TextView acWaijianTimes;
    @BindView(R.id.ac_waijian_line)
    TextView acWaijianLine;

    @BindView(R.id.ac_waijian_details_video)
    Button acDetailsVideo;
    @BindView(R.id.ac_waijian_btn_other)
    Button acWaijianBtnOther;
    @BindView(R.id.ac_waijian_btn_start)
    Button acWaijianBtnStart;
    @BindView(R.id.ac_waijian_btn_6m)
    Button acWaijianBtn6m;
    private int CHECK_MODE = 0;//检测模式 0:外检  1：路试 2：底盘动态
    private String Line = "01";//外检车道
    private String Item_Code = "";//F1外检,R1路试，DC底盘动态
    private String Login_name = "", login_IDNumber;//当前登录的检测员的名字
    private CarsInforModel carsInforModel;
    public WaiJianActivity instances = null;
    List<WaiJianAddRecordModel> WaiJianAddRecordModelList = new ArrayList<>();//外检增加记录list

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_waijian);
        ButterKnife.bind(this);
        setBackBtn();
        setTopTitle("外检车辆详情");
        instances = this;
        String logStrf = "\n" + "onCreate-----外检车辆详情-----WaiJianActivity--" + "\n";
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrf.getBytes());
//        i_3.putExtra("model_CarsInforModel", carsInforModel);
//        i_3.putExtra("Car_Line", Line);
//        i_3.putExtra("login_w_name", Login_name);
//        i_3.putExtra("login_IDNumber", login_IDNumber);
        carsInforModel = (CarsInforModel) getIntent().getSerializableExtra("model_CarsInforModel");
        login_IDNumber = getIntent().getExtras().getString("login_IDNumber", "");
        Login_name = getIntent().getExtras().getString("login_w_name", "");
        Line = getIntent().getExtras().getString("Car_Line", "");
        acWaijianLine.setText(Line);
        CHECK_MODE = BaseApplication.JIANCE_MODE;
//        switch (CHECK_MODE) {
//            case 0:
//                Item_Code = "F1";
//                break;
//            case 1:
//                Item_Code = "R1";
//                break;
//            case 2:
//                Item_Code = "DC";
//                break;
//        }
//
        if (carsInforModel != null) {
            //车牌号码
            if (!TextUtils.isEmpty(carsInforModel.getPlateRegion()) && !TextUtils.isEmpty(carsInforModel
                    .getPlateNO())) {
                acWaijianNumber.setText(carsInforModel.getPlateRegion() + carsInforModel.getPlateNO
                        ());
            }
            //检验次数```````
            if (!TextUtils.isEmpty(String.valueOf(carsInforModel.getTimes()))) {
                acWaijianTimes.setText(carsInforModel.getTimes() + "次");
            }
            if (!TextUtils.isEmpty(String.valueOf(carsInforModel.getSN()))) {
                acWaijianSN.setText(carsInforModel.getSN());
            }
//            //车身颜色
//            if (!TextUtils.isEmpty(String.valueOf(carsInforModel.getBodyColor())) && String.valueOf(carsInforModel.getBodyColor()) != "null") {
//                acVehicleCarColor.setText(VehicleCheckUtils.CarColorsFilter(carsInforModel
//                        .getBodyColor()));
//            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        List<WaiJianItemModel> books = WaiJianItemModel.listAll(WaiJianItemModel.class);
        if (books != null && books.size() > 0) {
            UtilsLog.e("WaiJianActivity----本地数据库查询----不为空==" + books.toString());
//            for (int i = 0; i < books.size(); i++) {
//                WaiJianItemModel.delete(books.get(i));
//            }
        } else {
            UtilsLog.e("WaiJianActivity----本地数据库查询----空==");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
        String logStrf = "\n" + "onDestroy-----外检车辆详情-----WaiJianActivity--" + "\n";
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrf.getBytes());
        instances = null;
    }

    @OnClick({R.id.ac_waijian_line, R.id.ac_waijian_details_video,
            R.id.ac_waijian_btn_other, R.id.ac_waijian_btn_start,
            R.id.ac_waijian_btn_6m})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_waijian_btn_6m:
                // 6米以上高度拍摄照片
                Line = acWaijianLine.getText().toString().trim();
                Intent i_12 = new Intent(this, Wai_6M_PhotoActivity.class);
                i_12.putExtra("model_CarsInforModel", carsInforModel);
                i_12.putExtra("Car_Line", Line);
                i_12.putExtra("login_w_name", Login_name);
                i_12.putExtra("login_IDNumber", login_IDNumber);
                startActivity(i_12);
                break;
            case R.id.ac_waijian_line:
                //检测线号
                showMyDialog(line_str, R.id.ac_waijian_line);
                break;
            case R.id.ac_waijian_details_video:
                Line = acWaijianLine.getText().toString().trim();
                Intent intent_video_1 = new Intent(instances, VideoListActivity.class);
                intent_video_1.putExtra("CarDetailsActivity_CarsInforModel", carsInforModel);
                intent_video_1.putExtra("CarDetailsActivity_acCarDetailsWaijianchedao", Line);
                intent_video_1.putExtra("video_type", 1);
                intent_video_1.putExtra("CarDetailsActivity_Line", Line);
                startActivity(intent_video_1);
                break;
            case R.id.ac_waijian_btn_other:
                //外检项目--更新车辆信息
                Line = acWaijianLine.getText().toString().trim();
                Intent intent_3 = new Intent(instances, WaiJianItemActivity.class);
                intent_3.putExtra("CarDetailsActivity_CarsInforModel", carsInforModel);
                intent_3.putExtra("CarDetailsActivity_Line", Line);
                intent_3.putExtra("login_w_name", Login_name);
                intent_3.putExtra("login_IDNumber", login_IDNumber);
                startActivity(intent_3);
                break;
            case R.id.ac_waijian_btn_start:
                //项目开始
                Line = acWaijianLine.getText().toString().trim();
//                Intent intent = new Intent(instances, WaiJianPhotoActivity.class);
//                intent.putExtra("CarDetailsActivity_CarsInforModel", carsInforModel);
//                intent.putExtra("CarDetailsActivity_acCarDetailsWaijianchedao",
//                        Line);
//                intent.putExtra("CarDetailsActivity_Line", Line);
//                intent.putExtra("login_w_name", Login_name);
//                intent.putExtra("login_IDNumber", login_IDNumber);
//                startActivity(intent);
//                if (!TextUtils.isEmpty(Item_Code)) {
//                    if (CHECK_MODE == 0) {
                getDataWaiJainStart();
//                        String logStr = "车牌号码：：" + carsInforModel.getPlateRegion() + carsInforModel
//                                .getPlateNO() + "\n" + "动作：点击项目开始按钮";
//                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
//                    } else {
//
//                    }
//                }
                break;
        }
    }

    private String[] line_str = new String[]{"01", "02", "03", "04", "05", "06"};
    private String[] fou_shi_str = new String[]{"否", "是"};

    private void showMyDialog(String[] str, final int id) {
//        BottomMenu bottomMenu= BottomMenu().build(this);
        BottomMenu.show(this, str, new OnMenuItemClickListener() {
            @Override
            public void onClick(String text, int index) {
                //返回参数 text 即菜单名称，index 即菜单索引
                UtilsLog.e("外检---showMyDialog==" + text + "===" + index);
                switch (id) {
                    case R.id.ac_waijian_line:
                        // 线号
                        acWaijianLine.setText(text);
                        break;
                }
            }
        }).setMenuTextInfo(new TextInfo().setBold(true).setFontColor(Color.BLACK).setGravity(Gravity.CENTER));
    }

    //外检检测开始
    private void getDataWaiJainStart() {
        CarItemStartModel carItemStartModel = new CarItemStartModel();
        carItemStartModel.setPlatformSN(carsInforModel.getPlatformSN());
        carItemStartModel.setPlateType(carsInforModel.getPlateType());
//        carItemStartModel.setPlateNO(carsInforModel.getPlateNO());
//        if (SharedPreferencesUtils.getAddress(this).equals("温州")) {
//            carItemStartModel.setPlateNO(carsInforModel.getPlateRegion()
//                    + carsInforModel.getPlateNO()
//                    + carsInforModel.getPlateExtra());
//        } else if (SharedPreferencesUtils.getAddress(this).equals("常山")) {
            carItemStartModel.setPlateNO(carsInforModel.getPlateRegion()
                    + carsInforModel.getPlateNO());
//        }
//        carItemStartModel.setPlateNO(carsInforModel.getPlateRegion() + carsInforModel.getPlateNO());
//        data.get(position).getPlateRegion() + data.get(position).getPlateNO()
        carItemStartModel.setVIN(carsInforModel.getVIN());
        carItemStartModel.setDetectionID(String.valueOf(carsInforModel.getID()));
        carItemStartModel.setLine(Line);
        carItemStartModel.setTest_times(carsInforModel.getTimes());
        carItemStartModel.setDetectionDevID("");
        carItemStartModel.setItemCode("F1");
        BaseApplication.W_ItemStart_time = "";
        BaseApplication.W_ItemStart_time = DateUtil.currentTime2();
//        carItemStartModel.setDetectionItemStartDate(DateUtil.currentTime2());
        carItemStartModel.setDetectionItemStartDate(BaseApplication.W_ItemStart_time);
        Gson gson = new Gson();
        String jsonStr = gson.toJson(carItemStartModel);

//        String upjson_url = "{[" + jsonStr.substring(1, jsonStr.length() - 1) + "]}";
        String upjson_url = "{" + jsonStr.substring(1, jsonStr.length() - 1) + "}";

        String logStr = "\n" + "外检项目开始----json" + "\n" +
                "json::" + upjson_url + "\n" +
                "URL::" + SharedPreferencesUtils.getIP(instances) + ITEM_START;
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        UtilsLog.e("getDataWaiJainStart---jsonStr==" + upjson_url);
        UtilsLog.e("getDataWaiJainStart---url==" + SharedPreferencesUtils.getIP(this) + ITEM_START);
        OkGo.<String>post(SharedPreferencesUtils.getIP(this) + ITEM_START)
                .tag(this)
                .upJson(upjson_url)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        CommonUtils.showLoadingDialog(instances, "加载中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body().trim();
                        UtilsLog.e("getDataWaiJainStart-result==" + result);
                        UtilsLog.e("getDataWaiJainStart-result=22=" + result.substring(1, result.length() -
                                1));
                        String logStr = "\n" + "外检项目开始----请求成功" + "\n" +
                                "result::" + response.body() + "\n" +
                                "URL::" + SharedPreferencesUtils.getIP(instances) + ITEM_START;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                        CommonUtils.hideLoadingDialog(instances);
                        String str = result.substring(1, result.length() - 1);
//                        if ("ok".equalsIgnoreCase(str)) {
                        Intent intent = new Intent(instances, WaiJianPhotoActivity.class);
                        intent.putExtra("CarDetailsActivity_CarsInforModel", carsInforModel);
                        intent.putExtra("CarDetailsActivity_acCarDetailsWaijianchedao",
                                Line);
                        intent.putExtra("CarDetailsActivity_Line", Line);
                        intent.putExtra("login_w_name", Login_name);
                        intent.putExtra("login_IDNumber", login_IDNumber);
                        intent.putExtra("start_time", BaseApplication.W_ItemStart_time);
                        startActivity(intent);
//                        Intent intent = new Intent(instances, WaiJianPhotoActivity.class);
//                        intent.putExtra("CarDetailsActivity_CarsInforModel", carsInforModel);
//                        intent.putExtra("CarDetailsActivity_acCarDetailsWaijianchedao",
//                                Line);
//                        intent.putExtra("CarDetailsActivity_Line", Line);
//                        intent.putExtra("login_w_name", Login_name);
//                        intent.putExtra("login_IDNumber", login_IDNumber);
//                        startActivity(intent);
                        finish();
//                        } else {
//                            Toast.makeText(instances, "上线失败，请重试", Toast.LENGTH_LONG)
//                                    .show();
//                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        String logStr2 = "\n" + "外检项目开始请求--onError" + "\n" +
                                "result::" + response.body();
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
                        UtilsLog.e("getDataWaiJainStart-onError==" + response.body());
                        CommonUtils.hideLoadingDialog(instances);
                    }
                });
    }

}
